package com.neu.questionnaireplus.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.io.IOUtils;
import org.springframework.http.MediaType;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

public class FileToMultipartFile {

  /**
  * @Description: 将文件转成 MultipartFile格式
  * @Param: [file]
  * @return: org.springframework.web.multipart.MultipartFile
  * @Author: ~FHX~ (:TSunS:)
  * @Date: 2022/10/31
  */
  public static MultipartFile getMultipartFile(File file) {
    FileItem item = new DiskFileItemFactory().createItem("file"
        , MediaType.MULTIPART_FORM_DATA_VALUE
        , true
        , file.getName());
    try (InputStream input = new FileInputStream(file);
        OutputStream os = item.getOutputStream()) {
      // 流转移
      IOUtils.copy(input, os);
    } catch (Exception e) {
      throw new IllegalArgumentException("Invalid file: " + e, e);
    }

    return new CommonsMultipartFile(item);
  }

}
